#include <16f877a.h> #include // khai bao thu vien chan, cong, thanh ghi #fuses nowdt,put,hs,noprotect,nolvp // tat bo dinh thoi, tri hoan 1 khoang tgian cho nguon on dinh, hoat dong o tan so cao(4 > 20Mhz), tat che do bao ve, khong thiet lap hoat dong o muc dien ap thap hay j do@@! #device *= 16 ADC = 10 //khai bao adc 10 bit #use delay(clock = 20000000) // su dung thach anh 20Mhz #use fast_io(b) // khai bao port b la ngo vao/ra //#use fast_io(c) // khai bao port c la ngo vao/ra #use fast_io(a) #use fast_io(d) #byte porta = 0x05 #byte portb = 0x06 //dia chi port c #byte portc = 0x07 //dia chi port c #byte portd = 0x08 int8 yeucau,tang,i; signed int sotang; int main() { set_tris_b(0xff); // set port B la ngo vao // set tris chi co tac dung khi khai bao fast io set_tris_c(0x00); // set port C la ngo ra set_tris_a(0x00); set_tris_d(0x00); tang = 1; porta = tang; portd = 0b00000000; while(1) { if(bit_test(RB0,0)) // kiem tra chan RB0 { yeucau = 1; sotang = yeucau - tang; if(sotang > 0) { for(i=0;i 0) { for(i=0;i 0) { for(i=0;i<=sotang;i++) { portd = 0b00000010; delay_ms(100); tang++; porta = tang; portd = 0b00000001; } } if(sotang < 0) { for(i=0;i 0) { for(i=0;i